/*   1:    */ package com.jetbrains.licenseService.util;
/*   2:    */ 
/*   3:    */

import com.jetbrains.licenseService.HeaderProperty;
import com.jetbrains.licenseService.requests.AbstractRequest;
import com.jetbrains.licenseService.requests.ObtainUserIdRequest;
import com.jetbrains.licenseService.responses.AbstractResponse;
import com.jetbrains.licenseService.responses.ObtainUserIdResponse;
import com.jetbrains.licenseService.responses.ResponseCode;
import org.jetbrains.annotations.Nullable;

import javax.net.ssl.*;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.*;
import java.lang.reflect.Method;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.security.*;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.*;

/*   4:    */
/*   5:    */
/*   6:    */
/*   7:    */
/*   8:    */
/*   9:    */
/*  10:    */
/*  11:    */
/*  12:    */
/*  13:    */
/*  14:    */
/*  15:    */
/*  16:    */
/*  17:    */
/*  18:    */
/*  19:    */
/*  20:    */
/*  21:    */
/*  22:    */
/*  23:    */
/*  24:    */
/*  25:    */
/*  26:    */
/*  27:    */
/*  28:    */
/*  29:    */
/*  30:    */
/*  31:    */
/*  32:    */
/*  33:    */
/*  34:    */
/*  35:    */
/*  36:    */
/*  37:    */
/*  38:    */
/*  39:    */
/*  40:    */
/*  41:    */
/*  42:    */
/*  43:    */
/*  44:    */
/*  45:    */ 
/*  46:    */ public class RpcUtil
/*  47:    */ {
/*  48:    */   private static final int a = 15000;
/*  49: 28 */   private static Set<String> b = new HashSet(Arrays.asList(new String[] { "actionName", "class" }));
/*  50:    */   private static SSLSocketFactory c;
/*  51:    */   private static SSLContext d;
/*  52:    */   private static GeneralSecurityException e;
/*  53:    */   
/*  54:    */   public static boolean isActionPropertyIgnored(String paramString)
/*  55:    */   {
/*  56: 33 */     return b.contains(paramString);
/*  57:    */   }
/*  58:    */   
/*  59:    */   static
/*  60:    */   {
/*  61:    */     try
/*  62:    */     {
/*  63: 40 */       SSLContext localSSLContext = SSLContext.getInstance("TLS");
/*  64: 41 */       localSSLContext.init(null, new TrustManager[] { new LicenseServerTrustManager(null) }, null);
/*  65: 42 */       c = localSSLContext.getSocketFactory();
/*  66: 43 */       d = localSSLContext;
/*  67:    */     }
/*  68:    */     catch (GeneralSecurityException localGeneralSecurityException)
/*  69:    */     {
/*  70: 46 */       e = localGeneralSecurityException;
/*  71:    */     }
/*  72:    */   }
/*  73:    */   
/*  74:    */   @Nullable
/*  75:    */   public static SSLContext getClientSSLContext()
/*  76:    */   {
/*  77: 52 */     return d;
/*  78:    */   }
/*  79:    */   
/*  80:    */   private static void a(String paramString1, String paramString2)
/*  81:    */     throws VerificationFailedException
/*  82:    */   {
/*  83:    */     boolean bool;
/*  84:    */     try
/*  85:    */     {
/*  86: 58 */       if ((!paramString2.startsWith("<!-- ")) || (!paramString2.endsWith(" -->"))) {
/*  87: 59 */         throw new VerificationFailedException("Validation failed");
/*  88:    */       }
/*  89: 62 */       byte[] arrayOfByte = new byte[(paramString2.length() - "<!-- ".length() - " -->".length()) / 2];
/*  90:    */       
/*  91: 64 */       int i = "<!-- ".length();
/*  92: 65 */       for (int j = 0; j < arrayOfByte.length; j++)
/*  93:    */       {
/*  94: 66 */         int k = a(paramString2.charAt(i++));
/*  95: 67 */         int m = a(paramString2.charAt(i++));
/*  96: 68 */         arrayOfByte[j] = ((byte)((k << 4) + m));
/*  97:    */       }
/*  98: 71 */       Signature localSignature = PublicKeys.createVerifyingSignature();
/*  99: 72 */       localSignature.update(paramString1.getBytes());
/* 100:    */       
/* 101: 74 */       bool = localSignature.verify(arrayOfByte);
/* 102:    */     }
/* 103:    */     catch (NoSuchAlgorithmException localNoSuchAlgorithmException)
/* 104:    */     {
/* 105: 77 */       throw new VerificationFailedException(localNoSuchAlgorithmException);
/* 106:    */     }
/* 107:    */     catch (InvalidKeyException localInvalidKeyException)
/* 108:    */     {
/* 109: 80 */       throw new VerificationFailedException(localInvalidKeyException);
/* 110:    */     }
/* 111:    */     catch (SignatureException localSignatureException)
/* 112:    */     {
/* 113: 83 */       throw new VerificationFailedException(localSignatureException);
/* 114:    */     }
/* 115: 85 */     if (!bool) {
/* 116: 86 */       throw new VerificationFailedException("Verification failed");
/* 117:    */     }
/* 118:    */   }
/* 119:    */   
/* 120:    */   private static int a(char paramChar)
/* 121:    */     throws VerificationFailedException
/* 122:    */   {
/* 123: 91 */     if (('0' <= paramChar) && (paramChar <= '9')) {
/* 124: 92 */       return paramChar - '0';
/* 125:    */     }
/* 126: 94 */     if (('A' <= paramChar) && (paramChar <= 'F')) {
/* 127: 95 */       return paramChar - 'A' + 10;
/* 128:    */     }
/* 129: 97 */     if (('a' <= paramChar) && (paramChar <= 'f')) {
/* 130: 98 */       return paramChar - 'a' + 10;
/* 131:    */     }
/* 132:100 */     throw new VerificationFailedException("Unexpected character in signature");
/* 133:    */   }
/* 134:    */   
/* 135:    */   @Nullable
/* 136:    */   private static String a(URL paramURL, Map<String, String> paramMap)
/* 137:    */     throws IOException, VerificationFailedException
/* 138:    */   {
/* 139:105 */     Pair localPair = a(paramURL, paramMap, Collections.emptyMap());
/* 140:    */     
/* 141:107 */     BufferedReader localBufferedReader = (BufferedReader)localPair.snd;
/* 142:108 */     if (((Integer)localPair.fst).intValue() != 200)
/* 143:    */     {
/* 144:109 */       localObject = new StringBuilder();
/* 145:110 */       for (str = localBufferedReader.readLine(); str != null; str = localBufferedReader.readLine())
/* 146:    */       {
/* 147:111 */         if (((StringBuilder)localObject).length() > 0) {
/* 148:112 */           ((StringBuilder)localObject).append("\n");
/* 149:    */         }
/* 150:114 */         ((StringBuilder)localObject).append(str);
/* 151:    */       }
/* 152:116 */       throw new IOException("Remote call failed: " + localObject);
/* 153:    */     }
/* 154:119 */     Object localObject = localBufferedReader.readLine();
/* 155:120 */     if (localObject == null) {
/* 156:121 */       return null;
/* 157:    */     }
/* 158:123 */     String str = localBufferedReader.readLine();
/* 159:124 */     if (str == null) {
/* 160:125 */       return null;
/* 161:    */     }
/* 162:127 */     localBufferedReader.close();
/* 163:    */     
/* 164:129 */     a(str, (String)localObject);
/* 165:    */     
/* 166:131 */     return str;
/* 167:    */   }
/* 168:    */   
/* 169:    */   public static String readFromUrl(URL paramURL)
/* 170:    */     throws IOException
/* 171:    */   {
/* 172:139 */     Object localObject = readFromUrl(paramURL, Collections.emptyMap(), Collections.emptyMap(), new ResultProcessor()
/* 173:    */     {
/* 174:    */       public Object processResult(int paramAnonymousInt, String paramAnonymousString)
/* 175:    */       {
/* 176:141 */         if (paramAnonymousInt == 200) {
/* 177:142 */           return paramAnonymousString;
/* 178:    */         }
/* 179:144 */         StringBuilder localStringBuilder = new StringBuilder();
/* 180:145 */         localStringBuilder.append("Http error ").append(paramAnonymousInt);
/* 181:146 */         if ((paramAnonymousString != null) && (!paramAnonymousString.isEmpty())) {
/* 182:147 */           localStringBuilder.append(": ").append(paramAnonymousString);
/* 183:    */         }
/* 184:149 */         return new IOException(localStringBuilder.toString());
/* 185:    */       }
/* 186:    */     });
/* 187:152 */     if ((localObject instanceof IOException)) {
/* 188:153 */       throw ((IOException)localObject);
/* 189:    */     }
/* 190:155 */     return (String)localObject;
/* 191:    */   }
/* 192:    */   
/* 193:    */   public static <Result> Result readFromUrl(URL paramURL, Map<String, String> paramMap1, Map<String, String> paramMap2, ResultProcessor<Result> paramResultProcessor)
/* 194:    */     throws IOException
/* 195:    */   {
/* 196:159 */     Pair localPair = a(paramURL, paramMap1, paramMap2);
/* 197:160 */     BufferedReader localBufferedReader = (BufferedReader)localPair.snd;
/* 198:161 */     StringBuilder localStringBuilder = new StringBuilder();
/* 199:    */     try
/* 200:    */     {
/* 201:163 */       for (String str = localBufferedReader.readLine(); str != null; str = localBufferedReader.readLine())
/* 202:    */       {
/* 203:164 */         if (localStringBuilder.length() > 0) {
/* 204:165 */           localStringBuilder.append("\n");
/* 205:    */         }
/* 206:167 */         localStringBuilder.append(str);
/* 207:    */       }
/* 208:    */     }
/* 209:    */     finally
/* 210:    */     {
/* 211:171 */       localBufferedReader.close();
/* 212:    */     }
/* 213:173 */     return paramResultProcessor.processResult(((Integer)localPair.fst).intValue(), localStringBuilder.toString());
/* 214:    */   }
/* 215:    */   
/* 216:    */   private static Pair<Integer, BufferedReader> a(URL paramURL, Map<String, String> paramMap1, Map<String, String> paramMap2)
/* 217:    */     throws IOException
/* 218:    */   {
/* 219:177 */     URLConnection localURLConnection = paramURL.openConnection();
/* 220:178 */     if ((localURLConnection instanceof HttpsURLConnection))
/* 221:    */     {
/* 222:179 */       if (e != null) {
/* 223:180 */         throw new IOException(e);
/* 224:    */       }
/* 225:182 */       localObject1 = (HttpsURLConnection)localURLConnection;
/* 226:183 */       ((HttpsURLConnection)localObject1).setSSLSocketFactory(c);
/* 227:184 */       ((HttpsURLConnection)localObject1).setHostnameVerifier(new HostnameVerifier()
/* 228:    */       {
/* 229:    */         public boolean verify(String paramAnonymousString, SSLSession paramAnonymousSSLSession)
/* 230:    */         {
/* 231:186 */           return true;
/* 232:    */         }
/* 233:    */       });
/* 234:    */     }
/* 235:190 */     localURLConnection.setReadTimeout(15000);
/* 236:191 */     localURLConnection.setConnectTimeout(15000);
/* 237:192 */     for (Object localObject1 = paramMap1.entrySet().iterator(); ((Iterator)localObject1).hasNext();)
/* 238:    */     {
/* 239:192 */       localObject2 = (Map.Entry)((Iterator)localObject1).next();
/* 240:193 */       localURLConnection.setRequestProperty((String)((Map.Entry)localObject2).getKey(), (String)((Map.Entry)localObject2).getValue());
/* 241:    */     }
/* 242:    */     Object localObject2;
/* 243:195 */     if (!paramMap2.isEmpty())
/* 244:    */     {
/* 245:196 */       localURLConnection.setDoOutput(true);
/* 246:197 */       localObject1 = new StringBuilder();
/* 247:198 */       for (localObject2 = paramMap2.entrySet().iterator(); ((Iterator)localObject2).hasNext();)
/* 248:    */       {
/* 249:198 */         Map.Entry localEntry = (Map.Entry)((Iterator)localObject2).next();
/* 250:199 */         if (((StringBuilder)localObject1).length() > 0) {
/* 251:200 */           ((StringBuilder)localObject1).append("&");
/* 252:    */         }
/* 253:202 */         ((StringBuilder)localObject1).append((String)localEntry.getKey()).append("=").append(URLEncoder.encode((String)localEntry.getValue(), "utf-8"));
/* 254:    */       }
/* 255:204 */       localObject2 = localURLConnection.getOutputStream();
/* 256:    */       try
/* 257:    */       {
/* 258:206 */         ((OutputStream)localObject2).write(((StringBuilder)localObject1).toString().getBytes("utf-8"));
/* 259:    */       }
/* 260:    */       finally
/* 261:    */       {
/* 262:209 */         ((OutputStream)localObject2).close();
/* 263:    */       }
/* 264:    */     }
/* 265:212 */     if ((localURLConnection instanceof HttpURLConnection))
/* 266:    */     {
/* 267:213 */       localObject1 = (HttpURLConnection)localURLConnection;
/* 268:214 */       int i = ((HttpURLConnection)localObject1).getResponseCode();
/* 269:215 */       if (i != 200) {
/* 270:216 */         return new Pair(Integer.valueOf(i), new BufferedReader(new StringReader(((HttpURLConnection)localObject1).getResponseMessage())));
/* 271:    */       }
/* 272:    */     }
/* 273:219 */     return new Pair(Integer.valueOf(200), new BufferedReader(new InputStreamReader(localURLConnection.getInputStream())));
/* 274:    */   }
/* 275:    */   
/* 276:    */   public static ObtainUserIdResponse executeRpc(ObtainUserIdRequest paramObtainUserIdRequest)
/* 277:    */     throws IOException, VerificationFailedException
/* 278:    */   {
/* 279:223 */     HashMap localHashMap = new HashMap();
/* 280:224 */     localHashMap.put("username", paramObtainUserIdRequest.getLoginName());
/* 281:225 */     localHashMap.put("password", paramObtainUserIdRequest.getPassword());
/* 282:226 */     (ObtainUserIdResponse)readFromUrl(new URL(ServiceConfig.getAuthorizationUrl()), Collections.emptyMap(), localHashMap, new ResultProcessor()
/* 283:    */     {
/* 284:    */       public ObtainUserIdResponse processResult(int paramAnonymousInt, String paramAnonymousString)
/* 285:    */       {
/* 286:228 */         if (paramAnonymousInt == 401) {
/* 287:229 */           return ObtainUserIdResponse.error("Wrong username or password", this.val$request);
/* 288:    */         }
/* 289:231 */         if (paramAnonymousInt != 200) {
/* 290:232 */           return ObtainUserIdResponse.error(paramAnonymousString, this.val$request);
/* 291:    */         }
/* 292:234 */         return new ObtainUserIdResponse(paramAnonymousString, ResponseCode.OK, "", this.val$request.getSalt());
/* 293:    */       }
/* 294:    */     });
/* 295:    */   }
/* 296:    */   
/* 297:    */   public static <T extends AbstractResponse> T executeRpc(AbstractRequest<T> paramAbstractRequest)
/* 298:    */     throws IOException, VerificationFailedException
/* 299:    */   {
/* 300:241 */     if ((paramAbstractRequest instanceof ObtainUserIdRequest))
/* 301:    */     {
/* 302:242 */       localObject1 = executeRpc((ObtainUserIdRequest)paramAbstractRequest);
/* 303:243 */       return localObject1;
/* 304:    */     }
/* 305:245 */     Object localObject1 = new StringBuilder();
/* 306:246 */     ((StringBuilder)localObject1).append(ServiceConfig.getLicenseServiceUrl()).append("/rpc/").append(paramAbstractRequest.getActionName());
/* 307:247 */     HashMap localHashMap = null;
/* 308:    */     try
/* 309:    */     {
/* 310:249 */       PropertyDescriptor[] arrayOfPropertyDescriptor = Introspector.getBeanInfo(paramAbstractRequest.getClass()).getPropertyDescriptors();
/* 311:250 */       int i = 1;
/* 312:251 */       for (Object localObject3 : arrayOfPropertyDescriptor)
/* 313:    */       {
/* 314:252 */         String str2 = localObject3.getName();
/* 315:253 */         if (!isActionPropertyIgnored(str2))
/* 316:    */         {
/* 317:256 */           Method localMethod = localObject3.getReadMethod();
/* 318:    */           
/* 319:258 */           Object localObject4 = localMethod.invoke(paramAbstractRequest, new Object[0]);
/* 320:259 */           if (localObject4 != null)
/* 321:    */           {
/* 322:262 */             String str3 = localObject4.toString();
/* 323:    */             
/* 324:264 */             HeaderProperty localHeaderProperty = (HeaderProperty)localMethod.getAnnotation(HeaderProperty.class);
/* 325:265 */             if (localHeaderProperty == null)
/* 326:    */             {
/* 327:266 */               if (i != 0)
/* 328:    */               {
/* 329:267 */                 ((StringBuilder)localObject1).append("?");
/* 330:268 */                 i = 0;
/* 331:    */               }
/* 332:    */               else
/* 333:    */               {
/* 334:271 */                 ((StringBuilder)localObject1).append("&");
/* 335:    */               }
/* 336:273 */               ((StringBuilder)localObject1).append(str2).append("=").append(URLEncoder.encode(str3, "utf-8"));
/* 337:    */             }
/* 338:    */             else
/* 339:    */             {
/* 340:276 */               String str4 = localHeaderProperty.value();
/* 341:277 */               if (str4.isEmpty()) {
/* 342:278 */                 str4 = str2;
/* 343:    */               }
/* 344:280 */               if (localHashMap == null) {
/* 345:281 */                 localHashMap = new HashMap();
/* 346:    */               }
/* 347:283 */               localHashMap.put(str4, str3);
/* 348:    */             }
/* 349:    */           }
/* 350:    */         }
/* 351:    */       }
/* 352:    */     }
/* 353:    */     catch (Exception localException)
/* 354:    */     {
/* 355:288 */       throw new VerificationFailedException("Error creating request", localException);
/* 356:    */     }
/* 357:291 */     URL localURL = new URL(((StringBuilder)localObject1).toString());
/* 358:292 */     String str1 = a(localURL, localHashMap == null ? Collections.emptyMap() : localHashMap);
/* 359:293 */     if (str1 == null) {
/* 360:294 */       throw new IOException("Server response is empty, incomplete or corrupted");
/* 361:    */     }
/* 362:296 */     ??? = (AbstractResponse)ResponseMarshaller.unmarshall(str1);
/* 363:298 */     if (((AbstractResponse)???).getSalt() != paramAbstractRequest.getSalt()) {
/* 364:299 */       throw new VerificationFailedException("Salt from server does not equals to salt in request");
/* 365:    */     }
/* 366:302 */     return ???;
/* 367:    */   }
/* 368:    */   
/* 369:    */   private static class LicenseServerTrustManager
/* 370:    */     implements X509TrustManager
/* 371:    */   {
/* 372:    */     public void checkServerTrusted(X509Certificate[] paramArrayOfX509Certificate, String paramString)
/* 373:    */       throws CertificateException
/* 374:    */     {
/* 375:308 */       if ((paramArrayOfX509Certificate == null) || (paramArrayOfX509Certificate.length == 0)) {
/* 376:309 */         throw new IllegalArgumentException("null or zero-length certificate chain");
/* 377:    */       }
/* 378:311 */       if ((paramString == null) || (paramString.isEmpty())) {
/* 379:312 */         throw new IllegalArgumentException("null or zero-length authentication type");
/* 380:    */       }
/* 381:314 */       for (X509Certificate localX509Certificate : paramArrayOfX509Certificate) {
/* 382:    */         try
/* 383:    */         {
/* 384:316 */           localX509Certificate.verify(PublicKeys.SERVER_CERT_SIGNATURE_PUBLIC_KEY);
/* 385:317 */           localX509Certificate.checkValidity();
/* 386:    */         }
/* 387:    */         catch (CertificateException localCertificateException)
/* 388:    */         {
/* 389:320 */           throw localCertificateException;
/* 390:    */         }
/* 391:    */         catch (Exception localException)
/* 392:    */         {
/* 393:323 */           throw new CertificateException(localException);
/* 394:    */         }
/* 395:    */       }
/* 396:    */     }
/* 397:    */     
/* 398:    */     public void checkClientTrusted(X509Certificate[] paramArrayOfX509Certificate, String paramString)
/* 399:    */       throws CertificateException
/* 400:    */     {}
/* 401:    */     
/* 402:    */     public X509Certificate[] getAcceptedIssuers()
/* 403:    */     {
/* 404:332 */       return null;
/* 405:    */     }
/* 406:    */   }
/* 407:    */   
/* 408:    */   public static abstract interface ResultProcessor<Result>
/* 409:    */   {
/* 410:    */     public abstract Result processResult(int paramInt, String paramString);
/* 411:    */   }
/* 412:    */ }



/* Location:           E:\idea\

 * Qualified Name:     com.jetbrains.licenseService.util.RpcUtil

 * JD-Core Version:    0.7.0.1

 */